/**
 * Created by evil on 16-12-21.
 */

var unFinishList;

$(function ()
{
    index_order.login();
    index_order.loadFinishOrderList();
});

var index_order = {
    login: function ()
    {
        $.post('/web/login', function (data, status)
        {
            if (status != 'success')
                dialog_light.show('警告', '登录失败，请刷新重试！');
            else
            {
                if (data == true)
                    toastr.success('登录成功！');

                index_order.websocket();
            }
        });
    },
    websocket: function ()
    {
        var url = window.location.host;
        ws.open(url + '/websocket');
        ws.listener(function (msg)
        {
            /**
             *  SEATLIST(101, "座位列表刷新"),
             *  ORDERLIST(102, "订单刷新"),
             *  COMPLETE(103, "菜品完成刷新");
             */

            if (msg == 102 || msg == 103)
                index_order.loadFinishOrderList();
        });
    },
    loadFinishOrderList: function ()
    {
        $.get('/index/order/get/order/list/unfinish', function (list)
        {
            unFinishList = list;
            var body = $('#order-list').find('tbody');
            body.empty();
            var html = "";
            for (var i = 0; i < list.length; i++)
            {
                html = '<tr>';
                html += '<td><a href="/index/order/id/' + list[i].orderId + '">' + list[i].orderId + '</a></td>';
                html += '<td>' + list[i].seatId.seat + '</td>';
                html += '<td>' + list[i].waiter.name + '</td>';
                html += '<td>' + list[i].createDateTime + '</td>';
                html += '<td><button class="btn btn-xs btn-info" content="' + list[i].orderId + '">菜单</button></td>';
                html += '</tr>';
                body.append(html);
            }
            index_order.orderItemClick();
        });
    },
    orderItemClick: function ()
    {
        var btn = $('#order-list').find('tr').find('td').find('button');
        btn.click(function ()
        {
            var id = $(this).attr('content');
            for (var i = 0; i < unFinishList.length; i++)
            {
                if (unFinishList[i].orderId == id)
                {
                    var item = unFinishList[i];
                    break;
                }
            }

            $('#order-item-dialog').modal('show');

            dialog(item);
        });
    }
};

function dialog(item)
{
    var dialog = $('order-item-dialog');
    $('.modal-title').html('订单ID：' + item.orderId + '&nbsp;&nbsp;&nbsp;座位号：' + item.seatId.seatId);

    var body = $('.modal-body').find('tbody');
    body.empty();
    var list = item.orderItemList;
    var html;
    for (var i = 0; i < list.length; i++)
    {
        html = '<tr>';
        html += '<td>' + list[i].mealId.name + '</td>';
        html += '<td>' + list[i].mealNum + '</td>';
        html += '<td>' + (list[i].mealTotal / 100).toFixed(2) + '</td>';
        html += '<td>' + list[i].state + '</td>';
        html += '</tr>';
        body.append(html);
    }

}